package librarianPanels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

import clientGUI.MainGUI;
import clientGUI.PanelDesign;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;

public class SearchABookPanel extends PanelDesign {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textFieldBookName;
	private JTextField textFieldAuthor;
	private JTextField textFieldCategory;
	private JTextField textFieldDescription;
	public static JTable table;
	/**
	 * Create the panel.
	 */

	public SearchABookPanel() 
	{
		setPreferredSize(new Dimension(644, 462));
		setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
		setBounds(0, 85, 644, 462);
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(25, 45, 607, 60);
		add(panel);
		panel.setLayout(null);
		textFieldBookName = new JTextField();
		textFieldBookName.setBounds(109, 1, 138, 22);
		panel.add(textFieldBookName);
		textFieldBookName.setColumns(10);
		
		final JCheckBox checkBoxBookName = new JCheckBox("Book Name:");
		checkBoxBookName.setBounds(0, 0, 105, 25);
		panel.add(checkBoxBookName);
		checkBoxBookName.setContentAreaFilled(false);
		checkBoxBookName.setBackground(new Color(0,0,0,0));
		
		final JCheckBox checkBoxAuthor = new JCheckBox("Author:");
		checkBoxAuthor.setBounds(0, 35, 71, 25);
		panel.add(checkBoxAuthor);
		checkBoxAuthor.setContentAreaFilled(false);
		checkBoxAuthor.setBackground(new Color(0,0,0,0));
		
		textFieldAuthor = new JTextField();
		textFieldAuthor.setBounds(109, 36, 138, 22);
		panel.add(textFieldAuthor);
		textFieldAuthor.setColumns(10);
		
		final JCheckBox checkBoxCategory = new JCheckBox("Category:");
		checkBoxCategory.setBounds(255, 0, 85, 25);
		panel.add(checkBoxCategory);
		checkBoxCategory.setContentAreaFilled(false);
		checkBoxCategory.setBackground(new Color(0,0,0,0));
		
		final JCheckBox chckbxDescription = new JCheckBox("Description:");
		chckbxDescription.setBounds(255, 35, 105, 25);
		panel.add(chckbxDescription);
		chckbxDescription.setContentAreaFilled(false);
		chckbxDescription.setBackground(new Color(0,0,0,0));
		
		textFieldCategory = new JTextField();
		textFieldCategory.setBounds(360, 1, 138, 22);
		panel.add(textFieldCategory);
		textFieldCategory.setColumns(10);
		
		textFieldDescription = new JTextField();
		textFieldDescription.setBounds(360, 36, 138, 22);
		panel.add(textFieldDescription);
		textFieldDescription.setColumns(10);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.setBounds(510, 0, 97, 60);
		panel.add(btnSearch);
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String selection = String.valueOf(checkBoxBookName.isSelected())+String.valueOf(checkBoxAuthor.isSelected())+String.valueOf(checkBoxCategory.isSelected())+String.valueOf(chckbxDescription.isSelected());
				if(!selection.contentEquals("falsefalsefalsefalse"))
				{
					String[] msgToServer=new String[6];
					msgToServer[0]="Search";
					msgToServer[1]=selection;
					msgToServer[2]="'%" + textFieldBookName.getText() + "%'"; 
					msgToServer[3]="'%" + textFieldAuthor.getText() + "%'";
					msgToServer[4]="'%" + textFieldCategory.getText() + "%'";
					msgToServer[5]="'%" + textFieldDescription.getText() + "%'";
					MainGUI.OBLClient.handleMessageFromClient(msgToServer);
				}
				else
				{
					JOptionPane.showMessageDialog(new JFrame(),	"ERROR - Please select search option!","Error",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		table = new JTable();
		table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"BookID", "Name", "Author", "Location", "Copys left", "Earliest Return Date"
			}
		));
		table.getTableHeader().setReorderingAllowed(false);
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(2).setResizable(false);
		table.getColumnModel().getColumn(3).setResizable(false);
		table.getColumnModel().getColumn(4).setResizable(false);
		table.getColumnModel().getColumn(5).setResizable(false);
		table.getColumnModel().getColumn(0).setPreferredWidth(52);
		table.getColumnModel().getColumn(1).setPreferredWidth(100);
		table.getColumnModel().getColumn(2).setPreferredWidth(100);
		table.getColumnModel().getColumn(3).setPreferredWidth(57);
		table.getColumnModel().getColumn(4).setPreferredWidth(66);
		table.getColumnModel().getColumn(5).setPreferredWidth(127);
		table.setBounds(50, 133, 534, 231);
		JScrollPane test = new JScrollPane(table);
		test.setVisible(true);
		test.setBounds(25, 118, 607, 255);
		add(test);
		JButton btnWatchBookIndex = new JButton("Watch Book Index");
		btnWatchBookIndex.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(table.getSelectedRow()!=-1)
				{
					String[] msgToServer=new String[2];
					msgToServer[0]="WatchIndex";
					msgToServer[1]=table.getModel().getValueAt(table.getSelectedRow(),0).toString();
					MainGUI.OBLClient.handleMessageFromClient(msgToServer);
				}
				else 
				{
					JOptionPane.showMessageDialog(new JFrame(),	"ERROR - Nothing selected!","Error",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnWatchBookIndex.setBounds(247, 387, 138, 45);
		add(btnWatchBookIndex);
	
		
		JLabel lblSearch = new JLabel("Search");
		lblSearch.setFont(new Font("Tahoma", Font.PLAIN, 29));
		lblSearch.setBounds(269, 1, 105, 49);
		add(lblSearch);
		remove(background);
		add(background);
	}

	/**
	 * Replaces the data of the table in this panel with the given Matrix.
	 * @param data
	 */
	public static void changeTableContent(String[][] data)
	{
		
		table.setModel(new DefaultTableModel(data,		new String[] {
				"BookID", "Name", "Author", "Location", "Copys left", "Earliest Return Date"
			}){
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		});
		table.getTableHeader().setReorderingAllowed(false);
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(2).setResizable(false);
		table.getColumnModel().getColumn(3).setResizable(false);
		table.getColumnModel().getColumn(4).setResizable(false);
		table.getColumnModel().getColumn(5).setResizable(false);
		table.getColumnModel().getColumn(0).setPreferredWidth(52);
		table.getColumnModel().getColumn(1).setPreferredWidth(100);
		table.getColumnModel().getColumn(2).setPreferredWidth(100);
		table.getColumnModel().getColumn(3).setPreferredWidth(57);
		table.getColumnModel().getColumn(4).setPreferredWidth(66);
		table.getColumnModel().getColumn(5).setPreferredWidth(127);
	}

}
